<?php
namespace App\Jobs;

use App\Models\User;
use App\Models\UserCode;
use App\Jobs\Job;
use Illuminate\Contracts\Mail\Mailer;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Bus\SelfHandling;
use Illuminate\Contracts\Queue\ShouldQueue;

class SendForgotEmail extends Job implements SelfHandling, ShouldQueue
{
  use InteractsWithQueue, SerializesModels;

  protected $user;

  /**
   * Create a new job instance.
   * @return void
   */
  public function __construct(User $user)
  {
    $this->user = $user;
  }

  /**
   * Execute the job.
   * @return void
   */
  public function handle(Mailer $mailer)
  {
  	$email = $this->user->email;
		$code = rand_str(6, 1);

		$row = new UserCode;
		$row->email = $email;
		$row->code = $code;
		$row->save();

    $mailer->send('emails.forgot', ['code' => $code], function ($m) use ($email) {
    	$m->from('kf@omeeting.com', '魅课网');
    	$m->to($email)->subject('找回登录密码');
    });
  }
}
